package edu.xjtu;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args)  {
	// write your code here
        String path="D:\\gitImage\\dataSet";
        List<File> fileList= getFiles(path);
        List<FileDetail> fileDetailList=getFileDetail(fileList);
        System.out.println(fileDetailList);

        generateCsvFile(fileDetailList);
    }

    private static void generateCsvFile(List<FileDetail> fileDetailList) {
        File outFile=new File("./fileList.csv");


        try{
            BufferedWriter writer=new BufferedWriter(new FileWriter(outFile));
            String outString="file_id,file_url,file_type,file_size\n";
            writer.write(outString);

            for (FileDetail fileDetail : fileDetailList){
                outString=fileDetail.getFileId()+","+fileDetail.getFileUrl()+","+fileDetail.getFileType()+","+fileDetail.getFileSize()+"\n";
                writer.write(outString);
            }
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static List<FileDetail> getFileDetail(List<File> fileList) {
        List<FileDetail> ret=new ArrayList<>();
        for (int i =0;i<fileList.size();i++){
            FileDetail fileDetail=new FileDetail();
            fileDetail.setFileId(i);
            fileDetail.setFileSize(fileList.get(i).length()+"");
            String fileName=fileList.get(i).getName();
            String[] nameArr=fileName.split("\\.");
            String suffix = nameArr[nameArr.length-1];
            fileDetail.setFileType(suffix);
            String path=nameArr[0];
            fileDetail.setFileUrl(path);
            ret.add(fileDetail);
        }
        return ret;
    }

    public static List<File> getFiles(String path) {
        List<File> files = new ArrayList<>();
        File file = new File(path);
        System.out.println(file);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                files.add(tempList[i]);
                //文件名，不包含路径
                String fileName = tempList[i].getName();
                System.out.println(fileName);
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，
            }
        }
        return files;
    }
}
